extends VBoxContainer
class_name PlayerContainer

@export var bridge_container_prefeb:PackedScene

@onready var character_image:TextureRect = %CharacterImage
@onready var health_bar: ProgressBar = %HealthBar
@onready var under_health_bar: ProgressBar = %UnderHealthBar
@onready var energy_bar: ProgressBar = %EnergyBar
@onready var h_circular_container: HCircularContainer = %HCircularContainer
@onready var label: Label = %Label


var character: Player:set = _on_set_character

func _ready() -> void:
	if scale.x == -1:
		h_circular_container.scale.x = -1


func _on_set_character(new_character:Player) -> void:
	if character != null:
		reset()
	
	# 安全检查：确保new_character不为null
	if not new_character:
		print("Warning: trying to set null character")
		return
	
	character = new_character
	new_character.energy_change.connect(_on_character_energy_change)
	new_character.health_component.health_changed.connect(_on_health_changed)
	if Global.current_setting.get("MODE",2) == 0:
		new_character.input_handler.bridge_switch.connect(on_bridge_switched)

	_on_character_bridge_changed(0)
	

func reset() -> void:
	character.energy_change.disconnect(_on_character_energy_change)
	character_image = null
	

func _on_health_changed(cur_percent: float) -> void:
	var tween := create_tween()
	tween.set_parallel()
	tween.tween_property(health_bar,"value",cur_percent,0.2)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(under_health_bar,"value",cur_percent,0.2).set_delay(0.1)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)


func _on_character_energy_change(cur_energy:int, max_energy:int) -> void:
	energy_bar.max_value = max_energy
	label.text = "%d/%d" % [cur_energy,max_energy]
	var tween := create_tween()
	tween.set_ignore_time_scale()
	tween.tween_property(energy_bar,"value",cur_energy,0.2)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)


func _on_character_bridge_changed(cur_index:int) -> void:
	# 安全检查：确保character不为null
	if not character:
		print("Warning: character is null in _on_character_bridge_changed")
		return
	
	var cur_deck = character.bridge_bag
	
	# 确保容器数量匹配
	while h_circular_container.get_child_count() != cur_deck.size():
		if h_circular_container.get_child_count() > cur_deck.size():
			h_circular_container.remove_item(h_circular_container.get_children()[-1])
		else:
			var bridge_container_instance := bridge_container_prefeb.instantiate()
			h_circular_container.add_item(bridge_container_instance)
	
	# 统一显示逻辑：所有角色都使用背包系统
	for i in cur_deck.size():
		var bridge_container = h_circular_container.get_children()[i]
		var bridge = cur_deck[i].instantiate()
		bridge_container.set_bridge(bridge)
		
	
	# 设置选中索引
	h_circular_container.selected_index = cur_index
	
func on_bridge_switched(index:int) -> void:
	#Global.main.current_npc = get_tree().get_first_node_in_group("npc")
	Global.main.current_npc.change_emotion(index as Npc.Emotion)

func change_with_emotion(emo:Npc.Emotion) -> void:
	for i in h_circular_container.get_children():
		i.shake()
	match emo:
		Npc.Emotion.CALM:
			h_circular_container.selected_index = 0
		Npc.Emotion.DEPRESSED:
			h_circular_container.selected_index = 1
		Npc.Emotion.MANIA:
			h_circular_container.selected_index = 2
		Npc.Emotion.HOPE:
			h_circular_container.selected_index = 3
			
	
func _on_character_bridge_use_failed() -> void:
	var selected_container = h_circular_container.get_selected_child()
	selected_container.shake()

func _on_health_bar_resized() -> void:
	if health_bar != null:
		under_health_bar.size = health_bar.size
		under_health_bar.global_position = health_bar.global_position
